// 导入 导出
// NodeJS 中导出 使用 module.exports = {} 和 exports.xxx = yyy

// 完整导出（完整导入）
// ES6 中使用export default 导出数据 使用 import xxx from './' 这种方式导入数据
// 按需导出（按需导入）
// ES6 中使用的是export const abc = 123 使用import { abc } from './' 导入数据

import axios from 'axios'

import { Toast } from 'vant'

import qs from 'qs'

import router from '@/router'

// 设置axios请求的根路径
axios.defaults.baseURL = 'http://www.lovegf.cn:9527/api'

// 添加响应拦截器
axios.interceptors.response.use(function (response) {

    console.log(response)
    if (response.data.status == -2) {
        localStorage.removeItem('zxgToken')
        localStorage.removeItem('userinfo')
        router.replace('/home')
    }
    return response;
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});

// 获取轮播图数据
export const getSliders = () => {
    return axios.get('/home/sliders').then(res => {
        if (res.data.status == 0) {
            return res.data.data
        } else {
            Toast(res.data.msg);
        }
    })
}

// 获取导航分类数据
export const getCategories = () => {
    return axios.get('/home/category').then(res => {
        if (res.data.status == 0) {
            return res.data.data
        } else {
            Toast(res.data.msg);
        }
    })
}

// 获取楼层商品数据
export const getFloorList = () => {
    return axios.get('/home/floorlist').then(res => {
        if (res.data.status == 0) {
            return res.data.data
        } else {
            Toast(res.data.msg);
        }
    })
}

// 获取商品列表
export const getProductList = params => {
    return axios.get('/product/list', {
        params
    }).then(res => {
        if (res.data.status == 0) {
            return res.data.data
        } else {
            Toast(res.data.msg);
        }
    })
}

// 获取商品详情
export const getProductDetail = params => {
    return axios.get('/product/detail', {
        params
    }).then(res => {
        if (res.data.status == 0) {
            return res.data.data
        } else {
            Toast(res.data.msg);
        }
    })
}

// 获取分类数据
export const getCategoryList = () => {
    return axios.get('/category/data').then(res => {
        if (res.data.status == 0) {
            return res.data.data
        } else {
            Toast(res.data.msg);
        }
    })
}

// 登录
export const login = (params) => {
    // username=admin&password=123456
    return axios.post('/user/login', qs.stringify(params)).then(res => {
        return res.data
    })
}

/**
 * 获取用户信息
 */
export const getUserInfo = () => {
    return axios.get('/user/info', {
        headers: {
            Authorization: localStorage.getItem('zxgToken')
        }
    }).then(res => {
        return res.data
    })
}

// 登录
export const logout = () => {
    return axios.post('/user/logout', null, {
        headers: {
            Authorization: localStorage.getItem('zxgToken')
        }
    }).then(res => {
        return res.data
    })
}

/**
 * 获取购物车数据
 */
export const getCarList = () => {
    return axios.get('/cart/list', {
        headers: {
            Authorization: localStorage.getItem('zxgToken')
        }
    }).then(res => {
        if (res.data.status == 0) {
            return res.data.data
        } else {
            Toast(res.data.msg);
        }
    })
}

/**
 * 获取推荐商品
 */
export const getRecommend = () => {
    return axios.get('/product/recommend', {
        headers: {
            Authorization: localStorage.getItem('zxgToken')
        }
    }).then(res => {
        if (res.data.status == 0) {
            return res.data.data
        } else {
            Toast(res.data.msg);
        }
    })
}

/**
 * 删除购物车
 */
export const delCar = params => {
    return axios.post('/cart/del', qs.stringify(params), {
        headers: {
            Authorization: localStorage.getItem('zxgToken')
        }
    }).then(res => {
        return res.data
    })
}

/**
 * 添加购物车
 */
export const addCar = params => {
    return axios.post('/cart/add', qs.stringify(params), {
        headers: {
            Authorization: localStorage.getItem('zxgToken')
        }
    }).then(res => {
        return res.data
    })
}